package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.constants.Constant;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: Jetwang
 * @date: 2022/4/10 17:02
 * @description: EmployeeController 员工控制器
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {
    private EmployeeService employeeService;

    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        // 1. 将密码进行md5进行处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 2. 查询数据库
        String name = employee.getUsername();
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, name);
        Employee employee01 = employeeService.getOne(lqw);
        // 3. 用户不存在
        if (employee01 == null) {
            return R.error("用户名不存在");
        }
        // 4. 校验密码,不相等时
        if (!employee01.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        // 5. 查看员工状态
        if (employee01.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        // 6. 账号正常,将id存入session中
        HttpSession session = request.getSession();
        session.setAttribute(Constant.LOGIN_SESSION, employee01.getId());
        return R.success(employee01);
    }


    /**
     * 退出系统
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request) {
        // 清除session保存的员工id
        request.getSession().removeAttribute(Constant.LOGIN_SESSION);
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        // 设置初始密码为123456，进行md5加密
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        // 设置时间
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long id = (Long) request.getSession().getAttribute(Constant.LOGIN_SESSION);
        employee.setCreateUser(id);
        employee.setUpdateUser(id);*/
        log.info("新增员工: {}", employee.toString());
        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    /**
     * 分页查询员工
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page> list(int page, int pageSize, @RequestParam(value = "name", required = false) String name) {
        log.info("page: {}, pageSize: {}, name: {}", page, pageSize, name);
        // 构造分页构造器
        Page<Employee> employeePage = new Page<>(page, pageSize);
        // 添加过滤条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(!StringUtils.isBlank(name), Employee::getName, name);
        // 添加排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        employeePage = employeeService.page(employeePage, lqw);
        return R.success(employeePage);
    }

    /**
     * 根据id更改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
        // 修改更新时间
        // employee.setUpdateTime(LocalDateTime.now());
        // 设置修改的用户
        // employee.setUpdateUser((Long)request.getSession().getAttribute(Constant.LOGIN_SESSION));
        employeeService.updateById(employee);
        return R.success("更新成功");
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable Long id) {
        log.info("根据id: {}查询员工信息...", id);
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        } else {
            return R.error("没有查询到对应的信息");
        }
    }
}
